package com.qixin.modules.eaLoan.service.impl;

import com.qixin.modules.eaLoan.entity.EaLoanbill;
import com.qixin.modules.eaLoan.entity.EaRepaymentbill;
import com.qixin.modules.eaLoan.entity.EaLoanbillDetail;
import com.qixin.modules.eaLoan.mapper.EaLoanbillDetailMapper;
import com.qixin.modules.eaLoan.mapper.EaLoanbillMapper;
import com.qixin.modules.eaLoan.mapper.EaRepaymentbillMapper;
import com.qixin.modules.eaLoan.service.IEaRepaymentbillService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Collection;
import java.util.Map;

/**
 * @Description: ea_repaymentbill
 * @Author: qixin-boot
 * @Date:   2022-01-21
 * @Version: V1.0
 */
@Service
public class EaRepaymentbillServiceImpl extends ServiceImpl<EaRepaymentbillMapper, EaRepaymentbill> implements IEaRepaymentbillService {

	@Autowired
	private EaRepaymentbillMapper eaRepaymentbillMapper;
	@Autowired
	private EaLoanbillDetailMapper eaLoanbillDetailMapper;
	@Autowired
	private EaLoanbillMapper eaLoanbillMapper;
	
	@Override
	@Transactional
	public void saveMain(EaRepaymentbill eaRepaymentbill, List<EaLoanbill> eaLoanbillDetailList) {
		eaRepaymentbillMapper.insert(eaRepaymentbill);
		// update-begin-author wjb 2022-02-11 08:40 for 还款单关联借款单操作
		if(eaLoanbillDetailList != null && eaLoanbillDetailList.size() > 0){
			for (EaLoanbill entity:eaLoanbillDetailList) {
				EaLoanbill eaLoanbill = eaLoanbillMapper.selectById(entity.getId());
				if (eaLoanbill != null){
					// 将还款单id值 放在 借款单的 ea_repaymentbill_id 字段中
					eaLoanbill.setEaRepaymentbillId(eaRepaymentbill.getId());
					eaLoanbillMapper.updateById(eaLoanbill);
				}
			}
		}
//		if(eaLoanbillDetailList!=null && eaLoanbillDetailList.size()>0) {
//			for(EaLoanbillDetail entity:eaLoanbillDetailList) {
//				//外键设置
//				entity.setEaRepaymentbillId(eaRepaymentbill.getId());
//				eaLoanbillDetailMapper.insert(entity);
//			}
//		}
		// update-end-author wjb 2022-02-11 08:40 for 还款单关联借款单操作
	}

	@Override
	@Transactional
	public void updateMain(EaRepaymentbill eaRepaymentbill,List<EaLoanbill> eaLoanbillDetailList) {
		eaRepaymentbillMapper.updateById(eaRepaymentbill);

		// update-begin-author wjb 2022-02-11 08:50 还款单关联借款单操作修改
		// 1、修改原借款单的引用状态
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("ea_repaymentbill_id",eaRepaymentbill.getId());
		List<EaLoanbill> eaLoanbills = eaLoanbillMapper.selectByMap(paramsMap);
		if (eaLoanbills != null && eaLoanbills.size() > 0){
			for (EaLoanbill entity: eaLoanbills) {
				// 把借款单的 ea_repaymentbill_id 字段值清除
				eaLoanbillMapper.updateByBillId(entity.getId());
			}
		}

		// 2、添加新的借款单引用
		if(eaLoanbillDetailList != null && eaLoanbillDetailList.size() > 0){
			for (EaLoanbill entity : eaLoanbillDetailList) {
				EaLoanbill eaLoanbill = eaLoanbillMapper.selectById(entity.getId());
				if (eaLoanbill != null){
					// 将还款单id值 放在 借款单的 ea_repaymentbill_id 字段中
					eaLoanbill.setEaRepaymentbillId(eaRepaymentbill.getId());
					eaLoanbillMapper.updateById(eaLoanbill);
				}
			}
		}

//		//1.先删除子表数据
//		eaLoanbillDetailMapper.deleteByMainId(eaRepaymentbill.getId());
//
//		//2.子表数据重新插入
//		if(eaLoanbillDetailList!=null && eaLoanbillDetailList.size()>0) {
//			for(EaLoanbillDetail entity:eaLoanbillDetailList) {
//				//外键设置
//				entity.setEaRepaymentbillId(eaRepaymentbill.getId());
//				eaLoanbillDetailMapper.insert(entity);
//			}
//		}

		// update-end-author wjb 2022-02-11 08:50 还款单关联借款单操作修改
	}

	@Override
	@Transactional
	public void delMain(String id) {
		// update-begin-author wjb 2022-02-11 09:08 取消借款单关联
		// 1、修改原借款单的引用状态
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("ea_repaymentbill_id",id);
		List<EaLoanbill> eaLoanbills = eaLoanbillMapper.selectByMap(paramsMap);
		if (eaLoanbills != null && eaLoanbills.size() > 0){
			for (EaLoanbill entity: eaLoanbills) {
				// 把借款单的 ea_repaymentbill_id 字段值清除
				eaLoanbillMapper.updateByBillId(entity.getId());
			}
		}
		// eaLoanbillDetailMapper.deleteByMainId(id);
		eaRepaymentbillMapper.deleteById(id);
		// update-end-author wjb 2022-02-11 09:08 还款单关联借款单操作修改
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			eaLoanbillDetailMapper.deleteByMainId(id.toString());
			eaRepaymentbillMapper.deleteById(id);
		}
	}
	
}
